<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>props的校验</h2>
      <child
        name="laotian"
        :age="18"
        sex="1"
        :weight="180"
        likes="xijiao"
      ></child>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const Child = {
        props: {
          name: String,
          // 可以用数据来表示允许多种类型
          age: [String, Number],

          // required:true表示必传
          // sex: String,
          sex: {
            type: String,
            required: true,
          },

          // default表示默认值
          weight: {
            type: Number,
            default: 280,
          },

          // 自定义类型校验函数
          likes: {
            type: String,
            validator(value) {
              // if (["xijiao", "anmo", "tangtou"].includes(value)) {
              //   return true;
              // } else {
              //   return false;
              // }
              return ["xijiao", "anmo", "tangtou"].includes(value);
            },
          },
        },
        template: `
          <div>这是child子组件</div>
          <div>name: {{ name }}</div>
          <div>age: {{ age }}</div>
          <div>sex: {{ sex }}</div>
          <div>weight: {{ weight }}</div>
          <div>likes: {{ likes }}</div>
        `,
      };

      Vue.createApp({
        data() {
          return {};
        },
        components: {
          Child,
        },
      }).mount("#app");
    </script>
  </body>
</html>
